//
//  MainViewController.swift
//  MYXG-Sina
//
//  Created by mac on 16/8/8.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.tintColor = UIColor.orangeColor()   //设置这个可以让文字和图片都是这个颜色
        addChildViewControllers()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //添加➕按钮
        tabBar.addSubview(composeButton)
        
        let rect = composeButton.frame.size
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
        
    }
    
    private func addChildViewControllers()
    {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(NullTableViewController(), title: "", imageName: "")
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    //override 代表重写父类的方法，Swift支持方法的重载，只要方法的参数个数或者数据类型不相同，那么系统就会认为是两个方法
    private func addChildViewController(childController: UIViewController, title: String, imageName: String)
    {
        
        
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        let naV = UINavigationController(rootViewController: childController)
        addChildViewController(naV)

    }
    
    // MARK: - 懒加载 加号按钮
    private lazy var composeButton: UIButton = {
        () -> UIButton
        in
        
        let btn = UIButton(imageName:"tabbar_compose_icon_add", backgroundImageName:"tabbar_compose_button")
        btn.addTarget(self, action: Selector("composeBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return  btn
    }()
    
    // MARK: - 如果直接设置该方法为私有的，外部点击回崩溃
    // Swift在编译时确定
    // 加上@abjc 可以支持动态派发，混编OC
    @objc private func composeBtnClick()
    {
        MYXGLog("点击➕")
    }

}
